var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');



var app = getApp();

Page({
  data: {
    deptArray: [],
    objDeptArray: [],
    deptId: 0,
    index: 0,
    username: '',
    mobile:''
  },
  bindPickerChange: function (e) {
    let that = this;
    this.setData({
      deptId: that.data.objDeptArray[e.detail.value].deptId,
      index: e.detail.value
    });
  },
  usernameInput: function (e) {
    let that = this;
    this.setData({
      username: e.detail.value,
    });
  },
  mobileInput: function (e) {
    let that = this;
    this.setData({
      mobile: e.detail.value,
    });
  },
  cleanMobile:function(){
    console.info('asd');
    let that = this;
    this.setData({
      mobile: '',
    });
  },
  cleanUsername: function () {
    console.info('cde');
    let that = this;
    this.setData({
      username: '',
    });
  },
  sbmitSysUserAdd : function(e){
    let that = this;
    if (that.data.index == 0){
      util.showErrorToast('请选择店铺');
      return false;
    }

    if (that.data.username == '') {
      util.showErrorToast('请输入账号');
      return false;
    }

    if (that.data.mobile == '') {
      util.showErrorToast('请输入手机号码');
      return false;
    }
    wx.showLoading({
      title: '提交中...',
      mask:true,
      success: function () {

      }
    });

    util.request(api.SysUserAdd, {
      mobile: that.data.mobile, 
      deptId: that.data.deptId, 
      username: that.data.username
    }).then(function (res) {
      if (res.errno === 0) {

        wx.hideLoading();

        wx.showToast({
          title: res.data,
          icon: 'success',
          duration: 2000,
          complete: function () {
            that.setData({
              index: 0,
              deptId: 0,
              username: '',
              mobile: ''
            });
          }
        });
      } else {
        util.showErrorToast(res.errmsg);
      }
      
    });
  },
  loadListData: function () {
    let that = this;

    util.request(api.DeptList).then(function (res) {
      console.info(res);
      if (res.errno === 0) {
        var deptArray = [] ;
        for (var i = 0; i < res.data.deptList.length; ++i) {
          deptArray[i] = res.data.deptList[i].name
        }
        that.setData({
          deptArray: deptArray,
          objDeptArray: res.data.deptList
        });
      }
    });
  },
  onLoad: function (options) {
    this.loadListData();
  },
  onReady: function () {

  },
  onShow: function () {

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭
  }
})